package com.speedtest.controller;

import com.speedtest.model.PingResponse;
import com.speedtest.model.UploadResponse;
import com.speedtest.service.SpeedTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api")
public class SpeedTestController {
    
    @Autowired
    private SpeedTestService speedTestService;
    
    @GetMapping("/ping")
    public ResponseEntity<PingResponse> ping() {
        PingResponse response = new PingResponse();
        response.setTimestamp(System.currentTimeMillis());
        response.setServer("SpeedTest Pro Server");
        response.setStatus("ok");
        
        return ResponseEntity.ok()
                .header(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate")
                .header(HttpHeaders.PRAGMA, "no-cache")
                .header(HttpHeaders.EXPIRES, "0")
                .body(response);
    }
    
    @GetMapping("/download/{size}")
    public ResponseEntity<Resource> downloadTest(@PathVariable String size) {
        try {
            int sizeMB = speedTestService.parseSizeParameter(size);
            Resource resource = speedTestService.generateTestData(sizeMB);
            
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
                    .header(HttpHeaders.CONTENT_LENGTH, String.valueOf(resource.contentLength()))
                    .header(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate")
                    .header(HttpHeaders.PRAGMA, "no-cache")
                    .header(HttpHeaders.EXPIRES, "0")
                    .header("Content-Disposition", "attachment; filename=test-data-" + size + ".bin")
                    .body(resource);
                    
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }
    
    @PostMapping("/upload")
    public ResponseEntity<UploadResponse> uploadTest(@RequestParam("file") MultipartFile file) {
        try {
            UploadResponse response = new UploadResponse();
            response.setMessage("Upload completed successfully");
            response.setSize(file.getSize());
            response.setTimestamp(System.currentTimeMillis());
            response.setFilename(file.getOriginalFilename());
            
            return ResponseEntity.ok()
                    .header(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate")
                    .body(response);
                    
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
    
    @GetMapping("/health")
    public ResponseEntity<String> health() {
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .body("{\"status\":\"ok\",\"timestamp\":\"" + System.currentTimeMillis() + "\"}");
    }
}